package cn.lijian.goods.admin.admin.category;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.lijian.goods.admin.admin.service.AdminService;
import cn.lijian.goods.book.service.BookService;
import cn.lijian.goods.category.domain.Category;
import cn.lijian.goods.category.service.CategoryService;

public class AdminCategoryServlet extends BaseServlet {

	private CategoryService categoryService = new CategoryService();
	private BookService bookService=new BookService();
	
	/**
	 * 添加一级分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String addParent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Category category = CommonUtils.toBean(request.getParameterMap(),
				Category.class);
		category.setCid(CommonUtils.uuid());
		categoryService.add(category);
		return findAll(request, response);

	}

	/**
	 * 添加二级分类第一步
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String addChildPre(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pid = request.getParameter("pid");
		List<Category> parents = categoryService.findParents();
		request.setAttribute("pid", pid);
		request.setAttribute("parents", parents);
		return "f:/adminjsps/admin/category/add2.jsp";
	}

	/**
	 * 添加二级分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String addChild(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Category category = CommonUtils.toBean(request.getParameterMap(),
				Category.class);
		category.setCid(CommonUtils.uuid());
		String pid = request.getParameter("pid");
		Category parent = new Category();
		parent.setCid(pid);
		category.setParent(parent);
		categoryService.add(category);
		return findAll(request, response);
	}

	/**
	 * 查询所有分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("parents", categoryService.findAll());
		return "f:/adminjsps/admin/category/list.jsp";
	}

	/**
	 * 修改一级分类 第一步
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String editParentPre(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("cid");
		Category category = categoryService.load(cid);
		request.setAttribute("parent", category);
		return "f:/adminjsps/admin/category/edit.jsp";

	}

	/**
	 * 修改一级分类 第二步
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String editParent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Category category = CommonUtils.toBean(request.getParameterMap(),
				Category.class);
		categoryService.edit(category);
		return findAll(request, response);
	}

	/**
	 * 修改二级分类 第一步
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String editChildPre(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("cid");
		Category category = categoryService.load(cid);
		List<Category> parents = categoryService.findParents();

		request.setAttribute("child", category);
		request.setAttribute("parents", parents);

		return "f:/adminjsps/admin/category/edit2.jsp";

	}

	public String editChild(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Category category = CommonUtils.toBean(request.getParameterMap(),
				Category.class);
		String pid = request.getParameter("pid");
		Category parent = new Category();
		parent.setCid(pid);
		category.setParent(parent);

		categoryService.edit(category);
		return findAll(request, response);
	}

	/**
	 * 删除一级分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String deleteParent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("cid");
		int count = categoryService.findChildrenCountByParent(cid);
		if (count > 0) {
			request.setAttribute("msg", "该分类下还有子分类，不能删除");
			return "f:/adminjsps/msg.jsp";
		} else {
			categoryService.delete(cid);
			return findAll(request, response);
		}
	}
	/**
	 * 删除二级分类
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String deleteChild(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("cid");
		int count = bookService.findBookCountByCategory(cid);
		if (count > 0) {
			request.setAttribute("msg", "该分类下还有图书，不能删除");
			return "f:/adminjsps/msg.jsp";
		} else {
			categoryService.delete(cid);
			return findAll(request, response);
		}
	}

}
